﻿using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IDAL;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.BLL
{
    public class RoleBLL : IRoleBLL
    {

        private IRoleDAL _roleDAL;

        public RoleBLL(IRoleDAL roleDal)
        {
            _roleDAL = roleDal;
        }

        public void Add(Roles model)
        {
            //IRoleDAL roleDAL = (IRoleDAL)IocContainer.Instance.Resolve<IRoleDAL>();

            _roleDAL.Add(model); 
        }

        public void Update(Roles model)
        {
            //IRoleDAL roleDAL = (IRoleDAL)IocContainer.Instance.Resolve<IRoleDAL>();
            _roleDAL.Update(model); 
        }

        public DataTable Query(string strWhere)
        {
            throw new NotImplementedException();
        }

        public DataTable Query(string strWhere, Hashtable parameter)
        {
            //IRoleDAL roleDAL = (IRoleDAL)IocContainer.Instance.Resolve<IRoleDAL>();
            return _roleDAL.Query(strWhere,parameter); 
        }
        public Roles GetRole(int roleId)
        {
            Roles role = new Roles();
            role.RoleId = roleId;
            Hashtable dtParameter = new Hashtable();
            dtParameter.Add("roleId", roleId);
            DataTable dtRole = _roleDAL.Query(" and RoleId=@roleId", dtParameter);
            if(dtRole!=null && dtRole.Rows.Count>=1)
            {
                role.RoleName = dtRole.Rows[0]["RoleName"].ToString();
                role.RoleDescription = dtRole.Rows[0]["RoleDescription"].ToString();
            }
            return role;
        }

       public void Delete(int roleId)
       {
            _roleDAL.Delete(roleId);
       }

       public Boolean IsExisit(string roleName,int roleId,Boolean needToIncludeRoleID)
       {
           Boolean isExisit = false;
           Hashtable dtParameter = new Hashtable();
           dtParameter.Add("RoleName", roleName);
           string sqlWhere = " and RoleName=@roleName";
           if(needToIncludeRoleID)
           {
               sqlWhere += " and RoleId<>@roleId ";
               dtParameter.Add("roleId", roleId);

           }
           DataTable dtRole = _roleDAL.Query(sqlWhere, dtParameter);
           if (dtRole != null && dtRole.Rows.Count >= 1)
           {
               isExisit= true;
           }
           return isExisit;
       }

       public void SetRoleAuthority(int roleId, int pageId, Dictionary<string, int> functionList)
       {
          _roleDAL.SetRoleAuthority(roleId,pageId,functionList);
       }
    }
}
